package com.prs.crm.action.person;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.person.JobTypeTitle;
import com.prs.crm.service.person.JobTypeTitleService;

public class JobTypeTitleAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private Integer[] ids;
	private PaginationSupport pagination;
	private JobTypeTitle jobTypeTitle;
	private JobTypeTitleService jobTypeTitleService;
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public JobTypeTitle getJobTypeTitle() {
		return jobTypeTitle;
	}
	public void setJobTypeTitle(JobTypeTitle jobTypeTitle) {
		this.jobTypeTitle = jobTypeTitle;
	}
	public JobTypeTitleService getJobTypeTitleService() {
		return jobTypeTitleService;
	}
	public void setJobTypeTitleService(JobTypeTitleService jobTypeTitleService) {
		this.jobTypeTitleService = jobTypeTitleService;
	}
	
	@Action("listJobTypeTitle")
	public String listJobTypeTitle(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(JobTypeTitle.class);
		pagination =this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getJobTypeTitleService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("editJobTypeTitle")
	public String editJobTypeTitle(){
		if(ids!=null){
			this.jobTypeTitle=this.getJobTypeTitleService().get(ids[0]);
		}
		return SUCCESS;
	}
	
	@Action(value="saveJobTypeTitle",results={
			@Result(name="success",type="redirect",location="listJobTypeTitle"),
			@Result(name="error",type="chain",location="editJobTypeTitle")
	})
	public String saveJobTypeTitle(){
		jobTypeTitle.setType(2);
		this.getJobTypeTitleService().save(jobTypeTitle);
		return SUCCESS;
	}
	@Action(value="saveJobDeafult",results={
			@Result(name="success",type="redirect",location="listJobTypeTitle"),
			@Result(name="error",type="chain",location="listJobTypeTitle")
	})
	public String saveJobDeafult(){
		if(ids!=null){
			this.jobTypeTitle=this.getJobTypeTitleService().get(ids[0]);
		}
		jobTypeTitle.setType(1);
		this.getJobTypeTitleService().submit(jobTypeTitle);
		return SUCCESS;
	}
	
	@Action(value="saveDelJobDeafult",results={
			@Result(name="success",type="redirect",location="listJobTypeTitle"),
			@Result(name="error",type="chain",location="listJobTypeTitle")
	})
	public String saveDelJobDeafult(){
		List<JobTypeTitle> list=this.getJobTypeTitleService().getAllJobTypeTitle();
		for (int i = 0; i < list.size(); i++) {
			JobTypeTitle job=list.get(i);
			if(job.getRemark()!=null){
				job.setType(2);
				this.getJobTypeTitleService().save(job);
			}
		}
		return SUCCESS;
	}
	@Action(value="removeJobTypeTitle",results={
			@Result(name="success",type="redirect",location="listJobTypeTitle"),
			@Result(name="error",type="chain",location="listJobTypeTitle")
	})
	public String removeJobTypeTitle(){
		if (ids==null) {
			this.addActionError("请选择一行数据进行操作!");
			return ERROR;
		}
		this.getJobTypeTitleService().batchRemove(ids);
		return SUCCESS;
	}
}
